#include "Communicate.h"
#include <QTimer>
#include "SerialPortQt.h"
#include "UDPSockQt.h"
#include "ProtocolMCU.h"
#include "ProtocolPC.h"

#ifdef POD_IS_205
#include "ProtocolPod205.h"
#else
#include "ProtocolPod613.h"
#endif //#ifdef POD_IS_205

#include "TerminalControl.h"

#include <QThread>
#include <memory>


bool CommInitQt()
{
    //用于通信的线程
    static QThread comThread;
    QObject::connect(&comThread, &QThread::finished, &comThread, &QObject::deleteLater);

    //MCU通信设置
    #ifdef MCU_COMM_TYPE_IS_QT
//    static SerialPortQt *commMCU = new SerialPortQt(DEV_NAME_MCU,&ProtocolMCU::GetInstance());
    static std::shared_ptr<SerialPortQt> commMCU = std::make_shared<SerialPortQt>(DEV_NAME_MCU,&ProtocolMCU::GetInstance());
    if(!(commMCU->SetOpt(19200,QSerialPort::NoFlowControl,QSerialPort::Data8,QSerialPort::OneStop,QSerialPort::NoParity)))
    {
        return false;
    }
//    QObject::connect(&(commMCU->m_portSer),&QSerialPort::readyRead,commMCU, &SerialPortQt::ReadAndParse);
    QObject::connect(&(commMCU->m_portSer),&QSerialPort::readyRead,commMCU.get(), &SerialPortQt::ReadAndParse);

    ProtocolMCU::GetInstance().SetCommTool(commMCU);
    ProtocolMCU::GetInstance().moveToThread(&comThread);//新线程
    QObject::connect(&comThread, &QThread::finished, &ProtocolMCU::GetInstance(), &QObject::deleteLater);
    #endif//#ifdef MCU_COMM_TYPE_IS_QT

    //PC通信设置
#ifdef PC_IS_ALIVE
#ifdef PC_COMM_TYPE_IS_QT
#ifdef PC_COMM_IS_SER
//    static SerialPortQt *commPC = new SerialPortQt(DEV_NAME_PC,&ProtocolPC::GetInstance());
    std::shared_ptr<SerialPortQt> commPC = std::make_shared<SerialPortQt>(DEV_NAME_PC,&ProtocolPC::GetInstance());
    if(!(commPC->SetOpt(115200,QSerialPort::NoFlowControl,QSerialPort::Data8,QSerialPort::OneStop,QSerialPort::EvenParity)))
    {
        return false;
    }
    QObject::connect(&(commPC->m_portSer),&QSerialPort::readyRead, commPC.get(), &SerialPortQt::ReadAndParse);
#else
//    static UDPSockQt *commPC  = new UDPSockQt(8888, &ProtocolPC::GetInstance());
    static std::shared_ptr<UDPSockQt> commPC = std::make_shared<UDPSockQt>(NET_PORT_PC, &ProtocolPC::GetInstance());
//    static auto commPC = std::make_shared<UDPSockQt>(8888, &ProtocolPC::GetInstance());
    if(!(commPC->Open()))
    {
        return false;
    }
    QObject::connect(&(commPC->m_socket),&QSerialPort::readyRead,commPC.get(), &UDPSockQt::ReadAndParse);
#endif//#ifdef PC_COMM_IS_SER
    ProtocolPC::GetInstance().SetCommTool(commPC);
    ProtocolPC::GetInstance().moveToThread(&comThread);//新线程
    QObject::connect(&comThread, &QThread::finished, &ProtocolPC::GetInstance(), &QObject::deleteLater);
#endif//#ifdef PC_COMM_TYPE_IS_QT
#endif//#ifdef PC_IS_ALIVE

    //Pod通信设置
#ifdef POD_COMM_TYPE_IS_QT
#ifdef POD_IS_205
    static std::shared_ptr<SerialPortQt> commPod = std::make_shared<SerialPortQt>(DEV_NAME_POD,&ProtocolPod205::GetInstance());
    if(!(commPod->SetOpt(115200,QSerialPort::NoFlowControl,QSerialPort::Data8,QSerialPort::OneStop,QSerialPort::EvenParity)))
    {
        return false;
    }
    QObject::connect(&(commPod->m_portSer),&QSerialPort::readyRead,commPod.get(), &SerialPortQt::ReadAndParse);
    ProtocolPod205::GetInstance().SetCommTool(commPod);
    ProtocolPod205::GetInstance().moveToThread(&comThread);//新线程
    QObject::connect(&comThread, &QThread::finished, &ProtocolPod205::GetInstance(), &QObject::deleteLater);
#else
    static std::shared_ptr<SerialPortQt> commPod = std::make_shared<SerialPortQt>(DEV_NAME_POD,&ProtocolPod613::GetInstance());
    if(!(commPod->SetOpt(115200,QSerialPort::NoFlowControl,QSerialPort::Data8,QSerialPort::OneStop,QSerialPort::EvenParity)))
    {
        return false;
    }
    QObject::connect(&(commPod->m_portSer),&QSerialPort::readyRead,commPod.get(), &SerialPortQt::ReadAndParse);
    ProtocolPod613::GetInstance().SetCommTool(commPod);
    ProtocolPod613::GetInstance().moveToThread(&comThread);//新线程
    QObject::connect(&comThread, &QThread::finished, &ProtocolPod613::GetInstance(), &QObject::deleteLater);
#endif//#ifdef POD_IS_205
#endif //#ifdef POD_COMM_TYPE_IS_QT


    //信息周期发送
    static QTimer timer40ms;
#ifdef PC_IS_ALIVE
#ifdef PC_COMM_TYPE_IS_QT
    QObject::connect(&timer40ms, &QTimer::timeout,&ProtocolPC::GetInstance(),&ProtocolPC::CycleSendOnce);
#endif//POD_COMM_TYPE_IS_QT
#endif//#ifdef PC_IS_ALIVE

#ifdef POD_IS_205
    QObject::connect(&timer40ms, &QTimer::timeout,&ProtocolPod205::GetInstance(),&ProtocolPod205::CycleSendOnce);
#else
    QObject::connect(&timer40ms, &QTimer::timeout,&ProtocolPod613::GetInstance(),&ProtocolPod613::CycleSendOnce);
#endif//#ifdef POD_IS_205



    comThread.start();
    timer40ms.start(40);


    return true;
}
